package com.sambo.inspection.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.google.gson.JsonElement;
import com.sambo.inspection.R;
import com.sambo.inspection.activity.support.BaseActivity;
import com.sambo.inspection.adapter.TreeAdapter;
import com.sambo.inspection.bean.PartitionBean;
import com.sambo.inspection.http.HttpClient;
import com.sambo.inspection.http.HttpResponseHandler;
import com.sambo.inspection.http.Urls;
import com.sambo.inspection.util.JsonUtil;
import com.sambo.inspection.util.TreeUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PartitionSearchActivity extends BaseActivity {

    ImageView ivBack;
    ListView listView;

    private TreeAdapter adapter;
    private List<PartitionBean> pointList = new ArrayList<>();
    private Map<String, PartitionBean> pointMap = new HashMap<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_partition_search);
        ivBack = findViewById(R.id.iv_back);
        listView = findViewById(R.id.listView);

        adapter = new TreeAdapter(this, pointList, pointMap);
        listView.setAdapter(adapter);

        initData();

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.onItemClick(position);
            }
        });
    }

    //初始化数据
    //数据特点：TreePoint 之间的关系特点   id是任意唯一的。    如果为根节点 PARENTID  为"0"   如果没有子节点，也就是本身是叶子节点的时候ISLEAF = "1"
    //  DISPLAY_ORDER 是同一级中 显示的顺序
    //如果需要做选中 单选或者多选，只需要给TreePoint增加一个选中的属性，在ReasonAdapter中处理就好了
    private void initData() {
//        pointList.clear();
//
//        PartitionBean bean = new PartitionBean("1", "津南区", "0", "0", 1);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("津南区描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("100", "八里台", "1", "0", 1);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台区描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("1001", "八里台一区", "100", "1", 1);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台一区描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("1002", "八里台二区", "100", "1", 2);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台二区描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("1003", "八里台三区", "100", "1", 3);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台三区描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("1004", "八里台四区", "100", "0", 4);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台四区描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("10041", "八里台四区A楼栋", "1004", "1", 1);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台四区A楼栋描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("10042", "八里台四区B楼栋", "1004", "1", 2);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("八里台四区B楼栋描述");
//        pointList.add(bean);
//
//        bean = new PartitionBean("101", "北闸口", "1", "1", 2);
//        bean.setId("1000");
//        bean.setAcreage(87);
//        bean.setWaterDemand(1290);
////        bean.setDesc("北闸口描述");
//        pointList.add(bean);
//
//        //对集合中的数据重新排序
//        updateData();

        Map<String, String> params = new HashMap<>();

        params.put("page", "1");
        params.put("limit", "200");

        HttpClient.get(this, Urls.API_SERVER_HOST + Urls.GET_DEPT_TREE_DATA, params,
                new HttpResponseHandler() {

                    @Override
                    public void onInnovationSuccess(JsonElement value) {
                        super.onInnovationSuccess(value);
                        pointList.clear();

                        PartitionBean[] response = JsonUtil.toObj(value.toString(), PartitionBean[].class);

                        if (response != null && response.length > 0) {
                            pointList.addAll(Arrays.asList(response));
                            updateData();
                        }

                    }
                }, null);
    }

    //对数据排序 深度优先
    private void updateData() {

        Map<String, String> searchLeafMap = new HashMap<>();

        for (PartitionBean treePoint : pointList) {
            pointMap.put(treePoint.getId(), treePoint);
            searchLeafMap.put(treePoint.getPid(), treePoint.getId());
        }

        for (PartitionBean treePoint : pointList) {
            if (searchLeafMap.containsKey(treePoint.getId())) {
                treePoint.setISLEAF("0");
            } else {
                treePoint.setISLEAF("1");
            }
        }

        Collections.sort(pointList, new Comparator<PartitionBean>() {
            @Override
            public int compare(PartitionBean lhs, PartitionBean rhs) {
                int llevel = TreeUtils.getLevel(lhs, pointMap);
                int rlevel = TreeUtils.getLevel(rhs, pointMap);
                if (llevel == rlevel) {
                    if (lhs.getPid().equals(rhs.getPid())) {  //左边小
                        return lhs.getDispOrder() > rhs.getDispOrder() ? 1 : -1;
                    } else {  //如果父辈id不相等
                        //同一级别，不同父辈
                        PartitionBean ltreePoint = TreeUtils.getTreePoint(lhs.getPid(), pointMap);
                        PartitionBean rtreePoint = TreeUtils.getTreePoint(rhs.getPid(), pointMap);
                        return compare(ltreePoint, rtreePoint);  //父辈
                    }
                } else {  //不同级别
                    if (llevel > rlevel) {   //左边级别大       左边小
                        if (lhs.getPid().equals(rhs.getId())) {
                            return 1;
                        } else {
                            PartitionBean lreasonTreePoint = TreeUtils.getTreePoint(lhs.getPid(), pointMap);
                            return compare(lreasonTreePoint, rhs);
                        }
                    } else {   //右边级别大   右边小
                        if (rhs.getPid().equals(lhs.getId())) {
                            return -1;
                        }
                        PartitionBean rreasonTreePoint = TreeUtils.getTreePoint(rhs.getPid(), pointMap);
                        return compare(lhs, rreasonTreePoint);
                    }
                }
            }
        });
        adapter.notifyDataSetChanged();
    }

}
